<?php
/**
 * Created by PhpStorm.
 * User: wei
 * Date: 2016/2/23
 * Time: 12:14
 */
namespace Api\Controller;
use Think\Controller;
class MessageController extends Controller {

    //获取消息
    public function getAllMessage(){
        //解决跨域
        echoHeader();
        $message=M('message');
        $type=I('post.type',0);       //参数0获取全部，1获取未读，2获取已读
        $username=I('post.user_id',null);

        if(!$username){
            $data=array(errcode=>1001,errmsg=>'缺少用户id');
            $this->ajaxReturn($data,'JSON');
        }

        if($type==1){
            $where['status']=array('eq',0);
            $data=$message->where($where)->select();
            $data=array(errcode=>200,errmsg=>$data);
            //更改未读为已读
            $message->status=1;
            $message->where('status=0')->save();
            $this->ajaxReturn($data,'JSON');
        }elseif($type==2){
            $where['status']=array('eq',1);
            $data=$message->where($where)->select();
            $data=array(errcode=>200,errmsg=>$data);
            $this->ajaxReturn($data,'JSON');
        } else{
            $where['status']=array('neq',2);
            $data=$message->where($where)->select();
            $data=array(errcode=>200,errmsg=>$data);
            $this->ajaxReturn($data,'JSON');
        }
    }


    //删除消息
    public function delMessage(){
        //解决跨域
        echoHeader();
        $messageid=I('post.messageid');
        $username=I('post.user_id');
        $sqldel=implode(',',$messageid);
        $message=M('message');
        $message->status=2;
        $where="user_id='$username' and id in($sqldel)";
        $resource=$message->where($where)->save();
        if($resource===false){
            $data=array(errcode=>403,errmsg=>'删除失败');
            $this->ajaxReturn($data,'JSON');
        }else{
            $data=array(errcode=>200,errmsg=>'删除成功');
            $this->ajaxReturn($data,'JSON');
        }


    }
}